package leetcode.editor.cn;

import java.util.*;
import leetcode.editor.common.*;

public class AddTwoNumbers {

    //leetcode submit region begin(Prohibit modification and deletion)
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            // 开始开发
            ListNode p = l1, q = l2; // 记录两个链表
            int count = 0; // 记录是否进位
            ListNode res = new ListNode(-1);
            ListNode resNode = res;
            while(p != null || q != null || count >0){
                int pN = p == null ? 0 : p.val;
                int qN = q == null ? 0 : q.val;
                if (p != null)
                p = p.next;
                if (q != null)
                q = q.next;
                int sum = pN + qN + count;
                count = (sum >= 10)?1:0;
                sum = sum % 10;
                ListNode node = new ListNode(sum);
                resNode.next = node;
                resNode = node;
            }
            return res.next;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

    
    public static void main(String[] args) {
        Solution solution = new AddTwoNumbers().new Solution();
        // put your test code here
        
    }
}